package com.mcxx.modules.system.controller;

import com.alibaba.fastjson.JSON;
import com.mcxx.modules.system.dto.RuleItemDTO;
import com.mcxx.modules.system.entity.SalvationRuleEntity;
import com.mcxx.modules.system.param.SalvationRuleParam;
import com.mcxx.modules.system.service.read.RuleObjectReadService;
import com.mcxx.modules.system.service.read.SalvationRuleReadService;
import com.mcxx.modules.system.service.write.SalvationRuleWriteService;
import com.mcxx.service.DefaultRestService;
import com.mcxx.validate.group.Update;
import com.yinhai.ta404.core.restservice.annotation.RestService;
import com.yinhai.ta404.core.restservice.requestbean.PageParam;
import com.yinhai.ta404.core.restservice.resultbean.Page;
import com.yinhai.ta404.core.validate.annotation.V;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.List;


@Slf4j
@Api("救助政策管理/救助政策")
@RestService("dataManagement/salvationRule/v2")
public class SalvationRuleController extends DefaultRestService {
    @Autowired
    private SalvationRuleReadService salvationRuleReadService;
    @Autowired
    private SalvationRuleWriteService salvationRuleWriteService;
    @Autowired
    private RuleObjectReadService ruleObjectReadService;


    @PostMapping("listRule")
    @ApiOperation("查询规则列表")
    public void listRule(SalvationRuleParam param, PageParam pageParam){
        log.info("开始查询规则列表，参数是businessType:{},pageParam: {}",JSON.toJSONString(param),JSON.toJSONString(pageParam));
        param.setTaOrgId(getCurUserAccount().getOrgId());
        Page page = salvationRuleReadService.listSalvationRule(param,pageParam);
        this.setPageBean(page);
        log.info("查询规则列表完成，结果是{}",JSON.toJSONString(page));
    }


    @PostMapping("getRule")
    @ApiOperation("查询指定公式")
    public void getRule(@V({"notnull"}) String id){
        log.info("查询查询指定公式，参数是id:{}",id);
        SalvationRuleEntity entity = salvationRuleReadService.getById(id);
        setData(entity);
        log.info("查询查询指定公式完成，结果是{}",entity);
    }


    @PostMapping("saveRule")
    @ApiOperation("保存救助规则")
    public void saveRule(SalvationRuleEntity entity){
        log.info("保存救助规则，参数为{}",JSON.toJSONString(entity));
        salvationRuleWriteService.saveRule(entity,getCurUserAccount());
        log.info("保存救助规则完成");
    }


    @PostMapping("updateRule")
    @ApiOperation("更新救助规则")
    public void updateRule(@Validated(Update.class) SalvationRuleEntity entity){
        log.info("更新救助规则，参数为{}",JSON.toJSONString(entity));

        SalvationRuleEntity entity1 = new SalvationRuleEntity();
        entity1.setId(entity.getId());
        entity1.setName(entity.getName());
        entity1.setChineseFormula(entity.getChineseFormula());
        salvationRuleWriteService.updateIgnoreNull(entity1);
        log.info("更新救助规则");
    }


    @PostMapping("deleteRule")
    @ApiOperation("删除救助规则")
    public void deleteRule(@V({"notnull"}) String id){
        log.info("删除救助规则，参数为{}",id);
        SalvationRuleEntity entity = new SalvationRuleEntity();
        entity.setId(id);
        salvationRuleWriteService.delete(entity);
        log.info("删除救助规则完成");
    }


    @PostMapping("updateStatus")
    @ApiOperation("更新救助规则状态")
    public void updateStatus(@V({"notnull"}) String id,@V({"notnull"}) String status){
        log.info("更新救助规则状态，参数为id：{},status：{}",id,status);
        SalvationRuleEntity entity = new SalvationRuleEntity();
        entity.setId(id);
        entity.setStatus(status);
        salvationRuleWriteService.updateIgnoreNull(entity);
        log.info("更新救助规则状态完成");
    }


    @PostMapping("getRuleItem")
    @ApiOperation("读取规则要素")
    public void getRuleItem(String businessType){
        log.info("读取规则要素，参数：{}", businessType);
        List<RuleItemDTO> ruleItemList = ruleObjectReadService.getRuleItem(businessType);
        setData(ruleItemList);
        log.info("读取规则要素，结果：{}", ruleItemList);
    }
}
